package com.cw.jeeyt.service.impl.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityUserRoleBo;
import com.cw.jeeyt.dao.intf.security.SecurityUserRoleDao;
import com.cw.jeeyt.dao.model.security.SecurityUserRoleDo;
import com.cw.jeeyt.service.converter.security.SecurityUserRoleConverter;
import com.cw.jeeyt.service.intf.security.SecurityUserRoleService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 用户角色业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SecurityUserRoleServiceImpl implements SecurityUserRoleService {

    private static final Logger logger = LoggerFactory.getLogger(SecurityUserRoleServiceImpl.class);
    @Autowired
    private SecurityUserRoleDao securityUserRoleDao;

    /**
     * 分页查询用户角色
     *
     * @param userRoleBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SecurityUserRoleBo> queryByPage(SecurityUserRoleBo userRoleBo, Integer startNum, Integer pageSize)
    throws Exception {

        if (userRoleBo == null) {
            logger.error("用户角色为空");
            return null;
        }
        PageInfo<SecurityUserRoleDo> pageInfo = securityUserRoleDao.queryByPage(
            SecurityUserRoleConverter.bo2Do(userRoleBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SecurityUserRoleConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻用户角色信息
     *
     * @param id
     * @return
     */
    @Override
    public SecurityUserRoleBo getUserRoleById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }

        return SecurityUserRoleConverter.do2Bo(securityUserRoleDao.getById(id));
    }

    /**
     * 新增用户角色
     *
     * @param userRoleBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SecurityUserRoleBo userRoleBo) throws Exception {

        if (userRoleBo == null) {
            logger.error("用户角色为空");
            return null;
        }
        SecurityUserRoleDo userRole = SecurityUserRoleConverter.bo2Do(userRoleBo);
        userRole.setId(SequenceTool.nextId());
        return securityUserRoleDao.insert(userRole);
    }

    /**
     * 更新用户角色信息
     *
     * @param userRoleBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SecurityUserRoleBo userRoleBo) throws Exception {

        if (userRoleBo == null) {
            logger.error("用户角色为空");
            return;
        }
        securityUserRoleDao.update(SecurityUserRoleConverter.bo2Do(userRoleBo));
    }

    /**
     * 删除用户角色
     *
     * @param userRoleBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SecurityUserRoleBo userRoleBo) throws Exception {

        if (userRoleBo == null) {
            logger.error("用户角色为空");
            return;
        }
        securityUserRoleDao.delete(SecurityUserRoleConverter.bo2Do(userRoleBo));
    }
}
